package com.itheima.edu.info.manager.system.dao;

import com.itheima.edu.info.manager.system.domain.Student;

/**
 * 操作学生数据的类
 * 数组的增删改查 遍历
 */
public class StudentDao implements BaseStudentDao{
    //创建容器
    private static Student[] stus=new Student[50];

    static{

        Student cai = new Student("0001", "蔡老师", 25, "1995-01-01");
        Student huang = new Student("0002", "黄老师", 23, "1997-01-01");
        Student wu = new Student("0003", "吴老师", 29, "1991-01-01");
        Student lu = new Student("0004", "鹿老师", 23, "1997-01-01");
        stus[0]=cai;
        stus[1]=huang;
        stus[2]=wu;
        stus[3]=lu;

    }
    /**
     * Dao添加方法
     *
     * @param student
     * @return
     */
    public boolean addStudent(Student student) {

        //在容器中找个位置
        int index=-1;
        for (int i = 0; i < stus.length; i++) {
            //stus[i]
            Student s=stus[i];
            if(s==null){
               /* stus[i]=student;
               return true;*/
               index=i;
               break;
            }
        }
        //将Student添加到容器中
        if(index==-1){
            return false;
        }else{
            stus[index]=student;
            return true;
        }

    }

    public Student[] findAllStudents() {

        return stus;
    }

    /**
     * 根据学号删除学生信息
     * @param id
     */
    public void deleteById(String id) {

        //1.根据学号查询索引
        int index=getIndex(id);
        //把他写成null
        stus[index]=null;

    }

    /**
     * 根据学号查询所在的索引
     * @param id
     * @return
     */
    public int getIndex(String id) {
        //遍历数组
        int index=-1;
        for (int i = 0; i < stus.length; i++) {
            //stus[i]
            Student s= stus[i];
            //判断学号是否存在
            if(s!=null&&s.getId().equals(id)){
                //存在 给出索引
                index=i;
                break;
                //return i;
            }
        }
            //不存在给-1
        return  index;
    }

    public void updateStudent(String id, Student student) {
        //获取id所在的索引
        int index=getIndex(id);
        //使用新对象替换之前的对象
        stus[index]=student;

    }
}
